package com.paishen.peiniwan.module.square;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.module.GlideModule;
import com.paishen.peiniwan.R;
import com.paishen.peiniwan.frame.http.PnwHttpManager;
import com.paishen.peiniwan.frame.http.entity.User;

/**
 * Created by Administrator on 2015/12/1.
 */
public final class SquareImageLayout extends FrameLayout {
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // For simple implementation, or internal size is always 0.
        // We depend on the container to specify the layout size of
        // our view. We can't really know what it is since we will be
        // adding and removing different arbitrary views and do not
        // want the layout to change as this happens.
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

        // Children are just made to fill our space.
        int childWidthSize = getMeasuredWidth();
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
        heightMeasureSpec = widthMeasureSpec;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public SquareImageLayout(Context context) {
        this(context, null);
    }

    private final ImageView imgAvatar;
    private final ImageView imgGender;
    private final TextView txTime;

    public SquareImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_main_square_image, this);
        imgAvatar = (ImageView) findViewById(R.id.img_avatar);
        imgGender = (ImageView) findViewById(R.id.img_gender);
        txTime = (TextView) findViewById(R.id.tx_time);
    }


    public void setData(User user,ColorDrawable defaultBackgroundColor) {
        Glide.with(getContext()).load(user.head_img_url).asBitmap().placeholder(defaultBackgroundColor).into(imgAvatar);
        txTime.setText(user.detail);
        imgGender.setImageResource(user.sex == PnwHttpManager.Square.SQUARE_FILTER_GENDER_MALE ? R.mipmap.square_list_gender_male : R.mipmap.square_list_gender_female);
    }


}